package main

import (
	//"github.com/gin-gonic/gin"
	"fmt"
	"html/template"
	"net/http"
)

type User struct {
	Name   string
	Gender string
	Age    int
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	// 2. 解析模板
	t, err := template.ParseFiles("./index.tmpl")

	if err != nil {
		fmt.Println("Parse template failed, err:%v", err)
		return
	}

	u1 := User{
		Name:   "小王子111",
		Gender: "男",
		Age:    28,
	}

	m1 := map[string]interface{}{
		"name":   "小王子222",
		"gender": "男",
		"age":    18,
	}

	// 3. 渲染模板
	err = t.Execute(w, map[string]interface{}{
		"u1": u1,
		"m1": m1,
	})

	if err != nil {
		fmt.Println("render template failed, err:%v", err)
		return
	}
}

func main() {
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		fmt.Println("Http server start failed, err:%v", err)
		return
	}
}
